package com.platform.modules.cpt.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.common.page.PageUtil;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.UUID;
import com.platform.modules.cpt.entity.CptAcceptanceEntity;
import com.platform.modules.cpt.service.CptAcceptanceService;
import com.platform.modules.quality.vo.CommonResult;
import com.platform.modules.sys.controller.AbstractController;
import com.platform.modules.sys.entity.SysOrgEntity;
import com.platform.modules.sys.entity.SysUserEntity;
import com.platform.util.DocUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * Controller
 *
 * @author biaohuaming
 * @date 2020-02-24 15:28:20
 */
@Api(tags = "竣工验收")
@RestController
@RequestMapping("cpt/acceptance")
public class CptAcceptanceController extends AbstractController {

    @Autowired
    private CptAcceptanceService cptAcceptanceService;
    
    @Autowired
    private DocUtil docUtil;
    
    /**
     * @descr 查询 竣工验收记录, 查询本机构<br>
     * @param currPage
     * @param limit
     * @return
     */
    @ApiOperation(value = "查询 竣工验收记录 PC")
    @PostMapping("/list")
    public RestResponse query(Integer currPage, Integer limit, @RequestParam(required = false) String prjName, 
    		@RequestParam(required = false) String cityNum, @RequestParam(required = false) String countyNum,
    		@RequestParam(required = false) String[] status) {
    	Map<String, Object> params = new HashMap<>();
//    	params.put("buildCorpId", getOrgNo());
//        params.put("page", PageUtil.getCurrPage(currPage));
//        params.put("limit", PageUtil.getLimit(limit));
//        params.put("cityNum", cityNum);
//        params.put("countyNum", countyNum);
//        params.put("prjName", prjName);
//        params.put("status", status);


        params.put("page", PageUtil.getCurrPage(currPage));
        params.put("limit", PageUtil.getLimit(limit));
        params.put("prjName", prjName);
        params.put("status", status);

        SysOrgEntity orgEntity = getOrg();
        // 当前是项目组织
        if(orgEntity == null){
            params.put("prjId", getOrgNo());
        }else{ // 实体组织
            // 如果是监管机构
            if("5".equals(orgEntity.getOrgType())){
                if(orgEntity.getOrgLevel()==2){
                    params.put("provinceNum", orgEntity.getProvinceNo());
                    params.put("countyNum", countyNum);
                    params.put("cityNum", cityNum);
                }else if(orgEntity.getOrgLevel()==3){
                    params.put("cityNum", orgEntity.getCityNo());
                    params.put("countyNum", countyNum);
                }else if(orgEntity.getOrgLevel()==4){
                    params.put("countyNum", orgEntity.getCountyNo());
                }
            }else if ("1".equals(orgEntity.getOrgType())){
                params.put("buildCorpId", getOrgNo());
            }else if ("9".equals(orgEntity.getOrgType())){
                params.put("sgCorpId", getOrgNo());
            }
        }


    	IPage<CptAcceptanceEntity> page = cptAcceptanceService.queryPage(params);
        return RestResponse.success().put("page", page);
    }

    /**
     * @param prjId
     * @return
     */
    @ApiOperation(value = "查询竣工验收数据")
    @GetMapping("/view")
    public RestResponse view(@RequestParam String prjId) {
    	Map<String, Object> params = getParams();
    	params.put("PRJ_ID", prjId);
    	CptAcceptanceEntity entity = cptAcceptanceService.queryOne(params);
        return RestResponse.success().put("data", entity);
    }
    
    /**
     * @descr 竣工验收申请-详情
     * @param id
     * @return
     */
    @ApiOperation(value = "竣工验收申请-详情")
    @GetMapping("/info")
    public RestResponse info(@RequestParam String cptAcceptanceId) {
    	CptAcceptanceEntity entity = cptAcceptanceService.getById(cptAcceptanceId);
        return RestResponse.success().put("data", entity);
    }
    
    /**
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "保存竣工验收数据")
    @PostMapping("save")
    public RestResponse save(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	SysOrgEntity sysOrgEntity = this.getOrg();
    	SysUserEntity sysUserEntity = this.getUser();
    	
    	//状态 0-未申请 1-监督条件审批中 2-监督中 3-编写竣工验收情况中 4-编写质量监督报告中 5-主管部门审批盖章中 6-编写竣工报告中 7-已完成
    	if(null==cptAcceptanceEntity.getStatus()) {
    		cptAcceptanceEntity.setStatus(0);
    		cptAcceptanceEntity.setBuildCorpId(sysOrgEntity.getOrgNo());
    		cptAcceptanceEntity.setBuildCorpName(sysOrgEntity.getOrgName());
    		cptAcceptanceEntity.setCreateTime(new Date());
    		cptAcceptanceEntity.setCreateUserId(sysUserEntity.getUserId());
    		cptAcceptanceEntity.setCreateUserName(sysUserEntity.getRealName());
    	}else {
    		//更新保存时, 因为页面格式化显示原因, 创建时间被修改. 因此赋值为空不修改创建时间
    		cptAcceptanceEntity.setCreateTime(null);
    		cptAcceptanceEntity.setUpdateTime(new Date());
    	}
    	cptAcceptanceService.saveOrUpdate(cptAcceptanceEntity);
        return RestResponse.success().put("cptAcceptance", cptAcceptanceEntity);
    }

    
    /**
     * 生成工程竣工验收监督通知书(后续盖章)
     * @param cptAcceptanceEntity
     * @return
     */
    @ApiOperation(value = "生成-工程竣工验收监督通知书(后续盖章)")
    @PostMapping("gcjgysjdtzsFile/create")
    public CommonResult createGcjgysjdtzsFile(@RequestBody CptAcceptanceEntity cptAcceptanceEntity) {
    	Map<String, Object> result = cptAcceptanceService.createGcjgysjdtzsFile(cptAcceptanceEntity);
        return CommonResult.success(result);
    }
    
    @ApiOperation(value = "下载-工程竣工验收监督通知书文件流")
    @GetMapping("gcjgysjdtzsFile/download")
    public void downloadGcjgysjdtzsFile(@RequestParam String cptId, HttpServletRequest request, HttpServletResponse response) {
    	cptAcceptanceService.downloadGcjgysjdtzsFile(cptId, request, response);
    }
    

    @ApiOperation(value = "盖章-工程竣工验收监督通知书文件")
    @PostMapping("gcjgysjdtzsFile/sign")
    public RestResponse signGcjgysjdtzsFile(HttpServletRequest request,MultipartFile file, 
    		@RequestParam String cptId, 
    		@RequestParam String attId,
    		@RequestParam(required=false) String fileName) {
    	
    	//获取已签章的合同
        String contentType = request.getContentType();
        
        if (contentType != null && contentType.toLowerCase().startsWith("multipart/")) {
        	cptAcceptanceService.signGcjgysjdtzsFile(request, cptId);
        }else {
        	RestResponse.error();
        }
        return RestResponse.success();
    }
    
    @ApiOperation(value = "查询-工程竣工验收监督通知书文件列表")
    @GetMapping("gcjgysjdtzsFile/list")
    public RestResponse getAcceptancePic(String cptId) {
        return RestResponse.success().put("list",  docUtil.getAttListByAttBusinessIdAndAttCode(cptId,"CPT_ACCPTANCE_FILES_GCJGYSJDTZS_SIGN"));
    }
}
